$(function () {

    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var recruit_datagrid =  $('#recruit_datagrid');
    //数据表格
    recruit_datagrid.datagrid({
        url:'/recruit/list.do',
        striped:true,
        singleSelect:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'cb',checkbox:true},
            {field:'id',hidden:true,width:100},
            {field:'planType',title:'计划类别',width:100,formatter:plantypeFormatter},
            {field:'plan',title:'计划名称',width:100,align:'right'},
            {field:'draftTime',title:'制定日期',width:100,align:'right'},
            {field:'staff_Id',title:'申请人',width:100,align:'right'},
            {field:'examine_Id',title:'审批人',width:100,align:'right'},
            {field:'examineState',title:'审批状态',width:100,align:'right',formatter:examineStateFormatter}
        ]]

    });

    //end
    var cmdObj = {

        //审核状态的改变
        audit:function(){
            //获取当前的操作的数据
            var row = recruit_datagrid.datagrid("getSelected");
            //判断是否选中数据
            if (!row){
                $.messager.alert("温馨提示","请先选择一条操作的数据");
                return;
            }
            $.messager.confirm("温馨提示","你确定要审核?",function (r) {

                if (r){

                    $.get("/recruit/audit.do?id="+row.id,function (data) {

                        if (data.success){

                            $.messager.confirm("温馨提示","审核成功",function (r) {

                                edit_dialog.dialog("close");
                                recruit_datagrid.datagrid("load");

                            });

                        }else{

                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            })
        },
        //添加按钮添加部门
        add : function () {

            edit_dialog.dialog("setTitle","招聘信息");

            edit_form.form("clear");

            edit_dialog.dialog("open");
        },
        //编辑按钮编辑
        edit : function () {
            var row = recruit_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选中一条数据","info");
                return;
            }
            edit_dialog.dialog("setTitle","编辑招聘");

            //在读取数据之前,清除缓存
            edit_form.form("clear");

            //加工回显数据
            if(row.dept){
                row["dept.id"]=row.dept.id;
            }
          /*  if(row.staff){
                row["staff.id"]=row.staff.id;
            }*/
            if(row.examine){
                row["examine.id"]=row.examine.id;
            }
            if(row.planType){
                row["planType.id"]=row.planType.id;
            }
            edit_form.form("load",row);
            edit_dialog.dialog("open");
        },
        //save按钮提交表单
        save : function () {

            var url = "";

            var id = $("#staffId").val();
            if(id){
                url = "/recruit/update.do";
            }else{
                url = "/recruit/save.do";
            }

            edit_form.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            recruit_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        },
        //刷新
        reload :function () {
            recruit_datagrid.datagrid("reload");
        },
        //高级查询
        //加载职工列表时,将多余查询条件一起发起请求
        searchT:function(){
            //加载列表数据时,携带额外参数
            recruit_datagrid.datagrid("load",{
                beginTime:$("#beginTime").val(),
                endTime:$("#endTime").val(),
                staffer:$("#staffer").val(),
                examiner:$("#examiner").val()
            })
        }

    };

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    });
    //end

    //dialog弹窗
    edit_dialog.dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: 750,
        height: 430
    });
    //end

});//end

//计划类别
function plantypeFormatter(value,row,index) {
    return value ? value.name: "";
}
//计划名称
function planFormatter(value,row,index) {
    return value?value.name:"";
}

//格式化员工状态
function examineStateFormatter(value,row,index) {
    if(value==0){
        return "<span style='color: red'>"+"未审核"+"</span>"
    }else if(value=1){
        return "<span style='color: green'>"+"已审核"+"</span>"
    }
}


